//
//  GLLimitDatePickView.m
//  AndCampus
//
//  Created by myMac on 2017/12/7.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "GLLimitDatePickView.h"

@interface GLLimitDatePickView()<UIPickerViewDataSource,UIPickerViewDelegate>{
    UIView *contentView;
    void(^backBlock)(NSString *);
}
@property(nonatomic,copy)NSString *selecttitle;
@property(nonatomic,strong)NSArray *currentdate;
@property(weak,nonatomic)UIPickerView *PickView;
@end

@implementation GLLimitDatePickView
- (instancetype)initDatePackerWithResponse:(void (^)(NSString *))block
{
    if (self=[super init]) {
        if (block) {
            backBlock = block;
            self.frame = [UIScreen mainScreen].bounds;
            [self getcurrentdate];
            [self setViewInterface];
        }
    }
    return self;
}
-(void) getcurrentdate{
    NSDate *currentdate=[NSDate date];
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM"];
    self.currentdate=[[formatter stringFromDate:currentdate] componentsSeparatedByString:@"-"];
}
- (void)setViewInterface {
    contentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, 300)];
    [self addSubview:contentView];
    //设置背景颜色为黑色，并有0.4的透明度
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    //添加白色view
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 40)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [contentView addSubview:whiteView];
    //添加确定和取消按钮
    for (int i = 0; i < 2; i ++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake((self.frame.size.width - 60) * i, 0, 60, 40)];
        [button setTitle:i == 0 ? @"取消" : @"确定" forState:UIControlStateNormal];
        if (i == 0) {
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        } else {
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        }
        [whiteView addSubview:button];
        [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 10 + i;
    }

    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, CGRectGetWidth(self.bounds), 260)];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    pickerView.backgroundColor = [UIColor colorWithRed:240.0/255 green:243.0/255 blue:250.0/255 alpha:1];
    [contentView addSubview:pickerView];
    self.PickView=pickerView;
    [self.PickView selectRow:[self.currentdate.lastObject integerValue]-1 inComponent:1 animated:YES];
    self.selecttitle = [NSString stringWithFormat:@"%@-%@",self.currentdate.firstObject,self.currentdate.lastObject];;
}

#pragma mark - Actions
- (void)buttonTapped:(UIButton *)sender {
    if (sender.tag == 10) {
        [self dismiss];
    } else {
        backBlock(self.selecttitle);
        [self dismiss];
    }
}

#pragma mark - pickerView出现
- (void)show {
    AppDelegate *app = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [app.window addSubview:self];
    [UIView animateWithDuration:0.4 animations:^{
        contentView.center = CGPointMake(self.frame.size.width/2, contentView.center.y - contentView.frame.size.height);
    }];
}
#pragma mark - pickerView消失
- (void)dismiss{

    [UIView animateWithDuration:0.4 animations:^{
        contentView.center = CGPointMake(self.frame.size.width/2, contentView.center.y + contentView.frame.size.height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - UIPickerViewDataSource UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (!component) {
        return 1;
    }
    return 12;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (!component) {
        return self.currentdate.firstObject;
    }
    else
    {
        return [NSString stringWithFormat:@"%lu月",row+1];
    }
    return @"";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (row>[self.currentdate.lastObject integerValue]) {
        [pickerView selectRow:[self.currentdate.lastObject integerValue]-1 inComponent:component animated:YES];
         self.selecttitle = [NSString stringWithFormat:@"%@-%@",self.currentdate.firstObject,self.currentdate.lastObject];
    }
    else
        self.selecttitle=[NSString stringWithFormat:@"%@-%lu",self.currentdate.firstObject,row+1];
//    self.selecttitle=self.dataArray[row];
}

@end
